<?php 

/**
 * @author Sergey S Yaglov
 * 
 * example:
 * 
 * <?php $tpl = $this->beginWidget('EJTemplateWidget')?>
 * Template by {$T.bold()} version <em>{$Q.version}</em>.
 * <?php $this->endWidget()?>
 * 
 * <div id="result"></div>
 * 
 * <script type="text/javascript">
 * 	jQuery("#result").setTemplateElement("<?php echo $tpl->id?>");
 * 	jQuery("#result").processTemplate("jTemplates");
 * </script>
 * 
 */

class EJTemplateWidget
extends CClipWidget
{
	
	public function registerClientScript(){
		$am = Yii::app()->assetManager;
		$cs = Yii::app()->clientScript;
		$url = $am->publish(
			dirname(__FILE__).DIRECTORY_SEPARATOR.'assets'.DIRECTORY_SEPARATOR.(YII_DEBUG
			? 'jquery-jtemplates_uncompressed.js'
			: 'jquery-jtemplates.js'
		));
		$cs->registerCoreScript('jquery');
		$cs->registerScriptFile($url);
	}
	
	public function run(){
		$this->registerClientScript();
		parent::run();
		$clips = $this->getController()->getClips();
		$content = $clips->itemAt($this->getId());
		$clips->remove($this->getId());
		echo CHtml::textArea($this->getId(), $content, array(
			'id' => $this->getId(),
			'style' => 'display:none',
		));
	}
	
}

?>